package org.csu.measure.domain;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class CodeCount {
        /**
         * 有效程序行数
         */
        private int normalLines = 0;
        /**
         * 空白行数
         */
        private int whiteLines = 0;

        /**
         * 注释行数
         */
        private int commentLines = 0;

        /**
         * 总行数
         */
        private int totalLines = 0;

        /**
         * 统计的代码的文件后缀名
         */
        public static String EXT_FILE = ".java";

        /**
         * 统计的文件夹
         */
        private String filename;

        public CodeCount(){};

        public CodeCount getResult(String filename) throws IOException {

            CodeCount codeCount = new CodeCount();
            this.filename = filename;
            File file = new File(filename);
            if (file.exists()) {
                if (file.isFile() && file.getName().endsWith(EXT_FILE))
                {
                    codeCount = parseFile(file);
                }
            }
            return codeCount;
        }

        /**
         * 解析文件
         */
        private CodeCount parseFile(File file) {
            // 判断此行是否为注释行
            boolean comment = false;
            int tempWhiteLines = 0;
            int tempCommentLines = 0;
            int tempNormalLines = 0;

            CodeCount codeCount = new CodeCount();
            try (
                    FileReader fr = new FileReader(file);
                    BufferedReader br = new BufferedReader(fr)) {
                String line = "";
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.matches("^[//s&&[^//n]]*$")) {
                        // 空行
                        tempWhiteLines++;
                    } else if (line.startsWith("/*")) {
                        // 判断此行为"/*"开头的注释行
                        tempCommentLines++;
                        //结尾不为*/的注释行
                        comment = !line.endsWith("*/");
                    } else if (comment == true && !line.endsWith("*/")) {
                        // 为多行注释中的一行（不是开头和结尾）
                        tempCommentLines++;
                    } else if (comment == true && line.endsWith("*/")) {
                        // 为多行注释的结束行
                        tempCommentLines++;
                        comment = false;
                    } else if (line.startsWith("//")) {
                        // 单行注释行
                        tempCommentLines++;
                    } else {
                        // 正常代码行
                        tempNormalLines++;
                    }
                }

                codeCount.setWhiteLines(tempWhiteLines);
                codeCount.setCommentLines(tempCommentLines);
                codeCount.setNormalLines(tempNormalLines);
                codeCount.setTotalLines(tempWhiteLines+tempCommentLines+tempNormalLines);

                return codeCount;

            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        public int totalLines(){
            return normalLines+whiteLines+commentLines;
        }

        public int getNormalLines() {
            return normalLines;
        }

        public void setNormalLines(int normalLines) {
            this.normalLines = normalLines;
        }

        public int getWhiteLines() {
            return whiteLines;
        }

        public void setWhiteLines(int whiteLines) {
            this.whiteLines = whiteLines;
        }

        public int getCommentLines() {
            return commentLines;
        }

        public void setCommentLines(int commentLines) {
            this.commentLines = commentLines;
        }

        public String getFilename() {
            return filename;
        }

        public void setFilename(String filename) {
            this.filename = filename;
        }

        public int getTotalLines() {
            return totalLines;
        }

        public void setTotalLines(int totalLines) {
            this.totalLines = totalLines;
        }
}